{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Pkg.status()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Pkg.add(\"Sigma\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Pkg.checkout(\"Sigma\") # Update to latest version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "using Sigma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "## Very simple reasoning\n",
    "## =====================\n",
    "person_is_man = flip(0.7)\n",
    "person_is_mortal = ifelse(person_is_man,\n",
    "                          true,\n",
    "                          flip(0.777))\n",
    "prob(person_is_mortal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "prob(person_is_man, person_is_mortal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "prob(person_is_mortal, person_is_man)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "## Paramter Estimation\n",
    "## ===================\n",
    "λ = uniform(0,2)\n",
    "x = mvexponential(λ, 3)\n",
    "observations = x == [0.083, 0.55, 2.37]\n",
    "λsamples = rand(λ, observations, 1000; precision = 0.001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plot(x=λsamples, Geom.histogram)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "using PyPlot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "## Sample from implicit equations\n",
    "## ==============================\n",
    "X = uniform(-2,2)\n",
    "Y = uniform(-2,2)\n",
    "Z = uniform(-2,2)\n",
    "\n",
    "# Constants control shape of elipsoid\n",
    "a = 2\n",
    "b = 1\n",
    "c = 0.5\n",
    "ellip_cond = ((X*X)/(a*a) + (Y*Y)/(b*b) + (Z*Z)/(c*c)) ==  1.0\n",
    "xyz = RandArray([X,Y,Z])\n",
    "samples_ellipsoid = rand(xyz, ellip_cond, 1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "scatter3D([s[1] for s in samples_ellipsoid], [s[2] for s in samples_ellipsoid], [s[3] for s in samples_ellipsoid])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "## Matrix Factorization\n",
    "## ====================\n",
    "matrix_to_factorize = [ 0.266358   0.136936   0.202717   0.332619\n",
    "                        0.0819769  0.0283733  0.0352097  0.088259\n",
    "                        0.632486   0.364566   0.55913    0.8302  \n",
    "                        1.008      0.468944   0.669905   1.20826 ]\n",
    "\n",
    "# Factors have prior probabilities\n",
    "# Create random array size 5x2, all uniformly distributed between 0 and 1\n",
    "W = mvuniform(0.0,1.0,4,2)\n",
    "H = mvuniform(0.0,1.0,2,4)\n",
    "\n",
    "W_sample, H_sample = rand((W,H), W*H == matrix_to_factorize; precision = 0.01)\n",
    "W_sample * H_sample"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.3.11",
   "language": "julia",
   "name": "julia-0.3"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.3.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
